package day01;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/2/27 10:18
 */
public class SingleLinkedList implements Iterable<Integer>{
    private Node head = null; // 头指针

    @Override
    public Iterator<Integer> iterator() {
        // 匿名内部类
        return new Iterator<Integer>() {
            Node p = head;
            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                int temp = p.value;
                p = p.next;
                return temp;
            }
        };
    }

    // 节点类-对外隐藏实现细节
    private static class Node {
        // 存储值
        int value;
        // 下一个节点
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 头部添加
     * @param value
     */
    public void addFirst(int value) {
        // 链表为空
        // head = new Node(value, null);
        // 链表非空
        head = new Node(value, head);
    }

    public void loop(Consumer<Integer> consumer) {
        Node p = head;
        while(p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    private Node findLast() {
        // 空链表
        if (head == null) {
            return null;
        }
        Node p = head;
        // for (p = head; p.next != null; p = p.next) {}
        while(p.next != null) {
            p = p.next;
        }
        return p;
    }

    /**
     * 尾部添加
     * @param value
     */
    public void addLast(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, null);
    }

    /*public void test() {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            System.out.println(p.value + "索引:" + i);
        }
    }*/

    private Node findNode(int index) {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            if (index == i) {
                return p;
            }
        }
        return null;
    }

    /**
     * 获取指定索引位置元素
     * @param index
     * @return
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new RuntimeException("未找到该索引位置元素!");
        }
        return node.value;
    }

    /**
     * 指定位置插入元素
     * @param index
     * @param value
     */
    public void insert(int index, int value) {
        if (index == 0) {
            addFirst(value);
            return;
        }
        // 找到该元素上一个节点
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw new RuntimeException("未找到该索引位置元素!");
        }
        prev.next = new Node(value, prev.next);
    }

    /**
     * 删除首节点
     */
    public void removeFirst() {
        if (head == null) {
            throw new RuntimeException("已经没元素了！");
        }
        head = head.next;
    }

    public void remove(int index) {
        // 移除第一个
        if (index == 0) {
            removeFirst();
            return;
        }
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw new RuntimeException("索引不合法!");
        }
        Node removed = prev.next;
        // 没有待删除节点
        if (removed == null) {
            throw new RuntimeException("索引不合法!");
        }
        prev.next = removed.next;
    }

    public void loop3(Consumer<Integer> before,
                      Consumer<Integer> after) {
        recursion(head, before, after);
    }

    // // 针对某个节点的操作
    private void recursion(Node curr, Consumer<Integer> before,
                           Consumer<Integer> after) {
        if (curr == null) {
            return;
        }
        before.accept(curr.value);
        recursion(curr.next, before, after);
        after.accept(curr.value);
    }
}

